/**
 * 
 */
package com.kelaite.procurement.controller;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.kelaite.procurement.dto.UserDTO;
import com.kelaite.procurement.entity.TCompany;
import com.kelaite.procurement.entity.TIndustry;
import com.kelaite.procurement.entity.TMaterialMasterCentral;
import com.kelaite.procurement.entity.TMaterialType;
import com.kelaite.procurement.entity.TUnit;
import com.kelaite.procurement.entity.TUser;
import com.kelaite.procurement.restful.BaseController;
import com.kelaite.procurement.service.CompanyService;
import com.kelaite.procurement.service.IndustryService;
import com.kelaite.procurement.service.MaterialMasterCentralService;
import com.kelaite.procurement.service.MaterialTypeService;
import com.kelaite.procurement.service.UnitService;
import com.kelaite.procurement.service.UserService;
import com.kelaite.procurement.utils.CurrentUserUtils;
import com.kelaite.procurement.utils.ExcelUtils;

/**
 * @author think
 *
 */
@Controller
@RequestMapping("/admin/")
public class AdminController extends BaseController {

	@Autowired
	private UserService userService;

	@Autowired
	private CompanyService companyService;

	@Autowired
	private IndustryService industryService;

	@Autowired
	private UnitService unitService;

	@Autowired
	private MaterialTypeService materialTypeService;

	@Autowired
	private MaterialMasterCentralService materialMasterCentralService;

	@InitBinder
	public void initBinder(WebDataBinder binder) {

		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		dateFormat.setLenient(true);
		binder.registerCustomEditor(Date.class, new CustomDateEditor(
				dateFormat, true));
	}

	@RequestMapping(value = "/index", method = { RequestMethod.GET,
			RequestMethod.POST })
	public String index(Model model) {

		// TODO
		UserDTO userDTO = CurrentUserUtils.getInstance().getUser();

		model.addAttribute("user", userDTO);

		return "admin/admin_index";
	}

	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/uploadExcel", method = { RequestMethod.GET,
			RequestMethod.POST })
	public String uploadExcel(@RequestParam("excelFile") MultipartFile file,
			Model model) {

		try {
			String fileName = file.getOriginalFilename();
			if (fileName.contains("TUser")) {
				List<TUser> userList = (List<TUser>) ExcelUtils
						.ReadExcelFile(file);
				for (int i = 0; i < userList.size(); i++) {
					this.userService.save(userList.get(i));
				}
			} else if (fileName.contains("TCompany")) {
				List<TCompany> companyList = (List<TCompany>) ExcelUtils
						.ReadExcelFile(file);
				for (int i = 0; i < companyList.size(); i++) {
					this.companyService.save(companyList.get(i));
				}
			} else if (fileName.contains("TUnit")) {
				List<TUnit> unitList = (List<TUnit>) ExcelUtils
						.ReadExcelFile(file);
				for (int i = 0; i < unitList.size(); i++) {
					this.unitService.save(unitList.get(i));
				}
			} else if (fileName.contains("TIndustry")) {
				List<TIndustry> industryList = (List<TIndustry>) ExcelUtils
						.ReadExcelFile(file);
				for (int i = 0; i < industryList.size(); i++) {
					this.industryService.save(industryList.get(i));
				}
			} else if (fileName.contains("TMaterialType")) {
				List<TMaterialType> materialTypeList = (List<TMaterialType>) ExcelUtils
						.ReadExcelFile(file);
				for (int i = 0; i < materialTypeList.size(); i++) {
					this.materialTypeService.save(materialTypeList.get(i));
				}
			} else if (fileName.contains("TMaterialMasterCentral")) {
				List<TMaterialMasterCentral> materialMasterCentralList = (List<TMaterialMasterCentral>) ExcelUtils
						.ReadExcelFile(file);
				for (int i = 0; i < materialMasterCentralList.size(); i++) {
					this.materialMasterCentralService
							.save(materialMasterCentralList.get(i));
				}
			}
		} catch (Exception e) {

		}
		return null;
	}

	@Override
	protected Class<?> getCurrentClass() {

		return AdminController.class;
	}

}
